/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package thimeeeee.View;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import javax.swing.*;
import sun.awt.image.FileImageSource;
import thimeeeee.Control.GameController;


/**
 *
 * @author ditu
 */
public class View extends Frame implements MouseListener
{
    public static final int ImageWidth = 50;
    public static final int ImageHeight = 50;

    private MenuState mState;
    
    public Panel fejlec;
    public Panel lablec;
    public JPanel torzs;
    public JPanel menuPanel;
    Panel deadPanel;
    
    public Canvas CJatekTer;
    public JPanel CInstr;
    public JPanel CAbout;


    public JButton buttonKill;
    public Label LbBomba;
    public Label LbDiamond;
    public Label LbLives;

    public JButton buttonStart;
    public JButton buttonAbout;
    public JButton buttonInst;

    public Image Buffer;
    public ViewDiamond d;
    public ViewField f;

    public int ICWidthSize = 600;
    public int ICHeightSize = 600;

    public GameController gameController;

    Image imgBg;
    public View(GameController GC)
    {
        super("Júz Kéz");

        gameController = GC;

        addWindowListener(GC);

        //fomenu betoltese
        mState = MenuState.StartUp;
        ChangePanel(mState);

//        Toolkit toolkit = Toolkit.getDefaultToolkit();
//        imgBg = toolkit.getImage("./image/load/background.jpg");
//
//        setSize(1024, 710);
//        setResizable(false);
        
        pack();
        setVisible(true);
        // repaint();
    }

    private void CreateMenuPanel()
    {
        if(menuPanel == null)
        {
            ImageIcon iconStart = new ImageIcon("image/load/menu_button_start.png");
            buttonStart  = new JButton();
            buttonStart.addActionListener(gameController);
            buttonStart.setActionCommand("jatek");

            buttonStart.setBorderPainted(false);
            buttonStart.setContentAreaFilled(false);
            buttonStart.setFocusPainted(false);

            buttonStart.setIcon(iconStart);

            ImageIcon iconInst = new ImageIcon("image/load/menu_button_instructions.png");
            buttonInst  = new JButton();
            buttonInst.addActionListener(gameController);
            buttonInst.setActionCommand("instructions");


            buttonInst.setBorderPainted(false);
            buttonInst.setContentAreaFilled(false);
            buttonInst.setFocusPainted(false);

            buttonInst.setIcon(iconInst);

            ImageIcon iconAbout = new ImageIcon("image/load/menu_button_about.png");
            buttonAbout  = new JButton();
            buttonAbout.addActionListener(gameController);
            buttonAbout.setActionCommand("about");


            buttonAbout.setBorderPainted(false);
            buttonAbout.setContentAreaFilled(false);
            buttonAbout.setFocusPainted(false);

            buttonAbout.setIcon(iconAbout);

            menuPanel = new JPanel();
            menuPanel.setLayout(new GridLayout(3,1));
            menuPanel.add(buttonStart);
            menuPanel.add(buttonInst);
            menuPanel.add(buttonAbout);
            // menuPanel.setOpaque(false);
        }
    }

    private void CreateInstructionsPanel()
    {
        if(CInstr == null)
        {
            CInstr = new ImagePanel("image/load/instructions.png");
            CInstr.addMouseListener(this);
        }
    }

    private void CreateAboutPanel()
    {
        if(CAbout == null)
        {
            CAbout = new ImagePanel("image/load/about.png");
            CAbout.addMouseListener(this);
        }
    }

    private void CreateDeadPanel()
    {
        if(deadPanel == null)
        {
            deadPanel = new Panel();
            deadPanel.setLayout(new GridLayout(1, 3));

            Label lbl = new Label();
            lbl.setText("Elfogytak az életeid!");

            Button btnRestart = new Button();
            btnRestart.setActionCommand("restart");
            btnRestart.setLabel("Újrakezd");
            btnRestart.addActionListener(gameController);

            Button btnFomenu = new Button();
            btnFomenu.setActionCommand("fomenu");
            btnFomenu.setLabel("Kilép");
            btnFomenu.addActionListener(gameController);

            deadPanel.add(lbl);
            deadPanel.add(btnRestart);
            deadPanel.add(btnFomenu);
        }
    }
    
    private void CreateLablec()
    {
        if(lablec == null)
        {
            LbBomba = new Label();
            LbDiamond = new Label();
            LbLives = new Label();
            buttonKill = new JButton();
            buttonKill.setActionCommand("kill");
            buttonKill.setText("Azonnali halál");
            buttonKill.addActionListener(gameController);

            lablec = new Panel();
            lablec.setLayout(new GridLayout(4,1));
            
            lablec.add(LbLives);
            lablec.add(LbDiamond);
            lablec.add(LbBomba);
            lablec.add(buttonKill);
        }
    }

    private void CreateTorzs()
    {
        if(torzs == null)
        {
            torzs = new JPanel();
            // torzs.setOpaque(false);
            torzs.setLayout(new FlowLayout());
        }
        add(torzs, BorderLayout.CENTER);
    }

    public void ChangePanel(MenuState state)
    {
        switch(state)
        {
            case StartUp:
                // jatek indulasa
                this.removeAll();
                CreateTorzs();
                pack();
                setVisible(true);
            case MainMenu:
                torzs.removeAll();
                if(lablec != null) lablec.removeAll();
                CreateMenuPanel();
                torzs.add(menuPanel);
                this.setSize(menuPanel.getWidth(), menuPanel.getHeight() + 30);
                mState = MenuState.MainMenu;
                break;
            case Instructions:
                torzs.removeAll();
                if(lablec != null) lablec.removeAll();
                CreateInstructionsPanel();
                torzs.add(CInstr);
                this.setSize(CInstr.getWidth(), CInstr.getHeight() + 30);
                mState = MenuState.Instructions;
                break;
            case About:
                torzs.removeAll();
                if(lablec != null) lablec.removeAll();
                CreateAboutPanel();
                torzs.add(CAbout);
                this.setSize(CAbout.getWidth(), CAbout.getHeight()+30);
                mState = MenuState.About;
                break;
            case Game:
                torzs.removeAll();
                
                CreateLablec();  // lablec letrehozasa
                this.add(lablec, BorderLayout.SOUTH); // lablec hozzaadasa az ablakhoz

                CJatekTer = new Canvas();
                CJatekTer.setSize(ICWidthSize, ICHeightSize);
                CJatekTer.setBackground(Color.BLACK);
                CJatekTer.addKeyListener(gameController);                
                torzs.add(CJatekTer);

                // mindenkeppen a torzs-hoz valo hozzaadas utan, kulonben Exception
                CJatekTer.createBufferStrategy(2); // dupla buffereles

                this.setSize(600, 600);
                mState = MenuState.Game;
                break;
            case PlayerDead:
                // torzs.removeAll();
                CreateDeadPanel(); // kerdezo panel letrehozasa
                this.remove(lablec);
                this.add(deadPanel, BorderLayout.SOUTH);
                this.setSize(this.getWidth(), CJatekTer.getHeight() + deadPanel.getHeight());
                mState = MenuState.PlayerDead;
                break;
        }

        torzs.repaint();
    }

    public void _ChangePanel(int action)
    {
        if(torzs == null)
            CreateTorzs();
        
        switch (action)
        {
            case 0: // game start
                //fomenu
                this.removeAll();
                torzs.removeAll();
                torzs.add(menuPanel);
                
                pack();
                setVisible(true);
                break;
            case 1:
                break;
            case 2: // jatekter betoltese
                torzs.remove(menuPanel);
                CJatekTer = new Canvas();
                CJatekTer.setSize(ICWidthSize, ICHeightSize);
                CJatekTer.setBackground(Color.BLACK);
                CJatekTer.addKeyListener(gameController);
                torzs.add(CJatekTer);
                //dupla buffer
                CJatekTer.createBufferStrategy(2);
                this.setSize(600, 600);
                break;
             case 3: // info ablak betoltese
                 torzs.remove(menuPanel);
                 
                 torzs.add(CAbout);
                 torzs.repaint();
                 break;

            case 4: // hasznalati utasitas
                torzs.removeAll();
                
                torzs.add(CInstr);
                torzs.repaint();
                break;
            case 5:
                //játékos életei elfogytak
                torzs.add(deadPanel);

                pack();
                setVisible(true);
        }
        torzs.repaint();
    }

    public void paint(char[][] map, int lives, int bomb, int reqDiamond, int diamond, int maxWidth, int maxHeight)
    {
        // paint(this.getGraphics());

        LbLives.setText("Életek: " + String.valueOf(lives));
        LbDiamond.setText("Gyémátok: " + String.valueOf(diamond)+"/"+String.valueOf(reqDiamond));
        LbBomba.setText("Bombák: " + String.valueOf(bomb));

        //játéktér méretének pontosítása
        CJatekTer.setSize(maxWidth * ImageWidth * 8/5, (maxHeight + 1) * ImageHeight / 2);
        this.setSize(maxWidth * ImageWidth * 8/5, (maxHeight + 1) * ImageHeight / 2 + 150);

        BufferStrategy bf = CJatekTer.getBufferStrategy();
        Graphics g = bf.getDrawGraphics();

        ViewElementBase element = null;
        for (int i = 0; i <maxHeight; i++){
            for (int j = 0; j < maxWidth; j++){
                element = null;
                switch (map[i][j])
                {
                    case 'n' :
                           //bomba és player2
                           element = new ViewPlayer2Bomb();
                           break;
                    case 'c' :
                           //bomba és player1
                           element = new ViewPlayer1Bomb();
                           break;
                    case 'k' :
                           //egyik játékos
                           element = new ViewPlayer1();
                           break;
                    case 'l' :
                           //másik játékos
                           element = new ViewPlayer2();
                           break;
                    case '-' :
                           //üres mező
                           element = new ViewField();
                           break;
                    case '#' :
                           //föld
                           element = new ViewSoil();
                           break;
                    case 'g' :
                           //gránit
                           element = new ViewGranite();
                           break;
                    case '*' :
                           //fal
                           element = new ViewWall();
                           break;
                    case 'e' :
                           //kijárat
                           element = new ViewExit();
                           break;
                    case 'r' :
                           //kő
                           element = new ViewRock();
                           break;
                    case 'd' :
                           //gyémánt
                           element = new ViewDiamond();
                           break;
                    case 'b' :
                           //bomba
                           element = new ViewBomb();
                           break;
                       //szörnyek
                    case '0' :
                           //sima
                           element = new ViewBeast___();
                           break;
                    case '1' :
                           //követő
                           element = new ViewBeastT__();
                           break;
                    case '2' :
                           //szaporodó
                           element = new ViewBeast_R_();
                           break;
                    case '3' :
                           //szaporodó-követő
                           element = new ViewBeastTR_();
                           break;
                    case '4' :
                           //gyémánt
                           element = new ViewBeast__D();
                           break;
                    case '5' :
                           //gyémánt-követő
                           element = new ViewBeastT_D();
                           break;
                    case '6' :
                           //gyémánt-szaporodó
                           element = new ViewBeast_RD();
                           break;
                    case '7' :
                           //gyémánt-szaporodó-követő
                           element = new ViewBeastTRD();
                           break;
                }

                DrawElement(element, i, j, g);
            }
        }
        //dupla buffer lapjainak cseréje
        bf.show();
    
    }

    private void DrawElement(ViewElementBase element, int i, int j, Graphics g){
            if (element != null)
                {
                    int posX, posY;
                    posX = j * ImageWidth;
                    posY = i * ImageHeight;

                    if (i % 2 == 1){
                        posX += ImageWidth / 5 * 4 - 3 + j * ImageWidth / 2;
                        posY -= ImageHeight / 2;
                    }
                    else{
                        posX += j * ImageWidth / 2;

                        //posY -= ImageHeight / 2;
                    }

                    if (i > 1){
                        posY = posY - (i / 2) * ImageHeight;
                    }

                    Graphics gElement = g.create(posX, posY, ImageWidth, ImageHeight);
                    element.Paint(gElement);
                }
        }

    public void mouseClicked(MouseEvent e)
    {
        if(mState == MenuState.Instructions ||
           mState == MenuState.About
                )
            ChangePanel(MenuState.MainMenu);
    }

    public void mousePressed(MouseEvent e) { }

    public void mouseReleased(MouseEvent e) { }

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e) { }
}
